var express = require('express');
var router = express.Router();
var { userModel, mouduleModel, roleModel, menuModel, departModel } = require("../module/user")


var multiparty = require("multiparty") //图片上传模块
/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', { title: 'Express' });
});




//获取模块信息
router.get('/moulePost', async (req, res) => {
    let mouduleList = await mouduleModel.find()
    //  console.log(menulist);
    res.send({ mouduleList: mouduleList })
});



//根据模块id查询菜单栏
router.get('/menuPost', async (req, res) => {
    console.log(req.query._id)//传递的
    _id = req.query._id
    console.log(_id);
    if (_id) {
        let menulist = await getmenus(req.query._id)
        //  console.log(menulist);
        res.send({ menulist: menulist })
    } else {
        res.send({ menuList: [] })
    }

});

async function getmenus(_id) {

    let arrmenu = (await mouduleModel.findOne({ _id: _id })).menu_id
    let mouduleMessage = (await mouduleModel.findOne({ _id: _id }))
    //  console.log(arrmenu);
    let menulist = await menuModel.aggregate([
        {
            $lookup: {
                from: "menu",
                localField: "_id",
                foreignField: "parent_id",
                as: "menulist"
            }
        }
    ])
    console.log(menulist)

    menulist = menulist.filter((item) => {
        return arrmenu.includes(item._id)
    })
    // console.log(menulist);
    menulist.forEach(item => {
        if (item.menulist.length == 0) {
            delete item.menulist
        }
    })
    return { menulist, mouduleMessage }
}
//显示部门结构
router.get('/departPost', async (req, res) => {
    let data = await departModel.find()
    data = data.map(item => ({ ...item._doc }))
    let lv1 = data.filter(item => item.parent === 1)
    let lv2 = data.filter(item => item.parent === 2)
    let lv3 = data.filter(item => item.parent === 3)
    let lv4 = data.filter(item => item.parent === 4)

    lv1.forEach(item1 => {
        item1.children = []
        item1.title=item1.departentName
        item1.key=item1._id
        item1.label=item1.departentName
        lv2.forEach(item2 => {
            item2.children = []
            item2.title=item2.departentName
            item2.key=item2._id
        item2.label=item2.departentName


            lv3.forEach(item3 => {
             item3.children = []
            item3.title=item3.departentName
            item3.key=item3._id
        item3.label=item3.departentName


                if (item3.parent_id.toString() == item2._id.toString()) {
                    item2.children.push(item3)
                }
                lv4.forEach(item4=>{
                    item4.title=item4.departentName
                    item4.key=item4._id
                    item4.label=item4.departentName


                    if (item4.parent_id.toString() == item3._id.toString()) {
                        item3.children.push(item4)
                    }
                })
            })
            if (item2.parent_id.toString() == item1._id.toString()) {
                item1.children.push(item2)
            }
        })
    })
    data = lv1
    console.log(data);
    res.send({ departlist: data })


});
//添加部门接口

router.post('/addDepart', async (req, res) => {
    
    let query = req.body
    console.log(query.parent_id)
    await departModel.create(query)
    res.send({code: 201, msg: '添加成功'})

});
//删除部门接口
router.delete('/delDepart', async (req, res,) => {
    let departentName = req.query.departentName
    await departModel.deleteOne({departentName: departentName})
    res.send({code: 201, msg: '删除成功'})
});


//上传图片
router.post("/addimg", async (req, res) => {
    let form = new multiparty.Form()

    form.uploadDir = "upload"
    form.parse(req, (err, formData, imgData) => {
        imgpath = imgData.null[0].path
        console.log(imgpath);
        res.send({ imgpath })
    })
})


//角色列表

module.exports = router;
